﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDataDB;
using GDataDB.Linq;
using TPTecnicas.MessageProcessor.Business;
using TPTecnicas.MessageProcessor.Persistence;
using TPTecnicas.MessageProcessor.Business.Repositories;
using TPTecnicas.MessageProcessor.Common;

namespace TPTecnicas.MessageProcessor.Persistence
{
    public class GoogleDocContext : IUniversityContext
    {
        public string usuario { get; set; }// el usuario del google docs
        public string password { get; set; }
        public string nombreBD { get; set; }
        public string codigoMateria { get; set; }

        public void AlumnoAltaMateria(string padron, string nombre, string identificador, string codigoMateria)
        {
            usuario = "tecnicas.de.diseno.2012@gmail.com";//de prueba
            password = "tecnicas2012";//de prueba
            nombreBD = "TecnicasDB";//de prueba
           
            IDatabaseClient client = new DatabaseClient(usuario, password);
            IDatabase db = client.GetDatabase(nombreBD) ?? client.CreateDatabase(nombreBD);
            List<Period> periods = new List<Period>();
            Period primerCuat = new Period();
            AcademicPeriod periodo = new AcademicPeriod();
  
            Student alumno = new Student();
                alumno.Name = nombre;
                alumno.Padron = padron;
                alumno.Email = identificador;
                alumno.Cuatrimestre = periodo.getCurrentPeriod();
            ITable<Student> table = db.GetTable<Student>(codigoMateria) ?? db.CreateTable<Student>(codigoMateria);
            table.Add(alumno);
            
            
        }

        /// <summary>
        /// Checks if user is registered in current semester
        /// </summary>
        /// <param name="message">id or padron</param>
        /// <returns>Wheter or not the user is registered</returns>
        public bool UsuarioRegistrado(string identificador)
        {
            IDatabaseClient client = new DatabaseClient(usuario, password);
            IDatabase db = client.GetDatabase(nombreBD) ?? client.CreateDatabase(nombreBD);
            Student alumno = new Student();
            alumno.Email = identificador;
            var table = db.GetTable<Student>(codigoMateria) ?? db.CreateTable<Student>(codigoMateria);
            var rows = from p in table.AsQueryable()
                       where p.Email == alumno.Email
                       select p;

            if (rows.ToList().Count() > 0)
            {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// Checks if user has group in current semester
        /// </summary>
        /// <param name="message">id or padron</param>
        /// <returns>Wheter or not the user has a group</returns>
        public bool UsuarioEnGrupo(string identificador)
        {
            IDatabaseClient client = new DatabaseClient(usuario, password);
            IDatabase db = client.GetDatabase(nombreBD) ?? client.CreateDatabase(nombreBD);
            Student alumno = new Student();
            alumno.Email = identificador;
            var table = db.GetTable<Student>("Grupos") ?? db.CreateTable<Student>("Grupos");
            var rows = from p in table.AsQueryable()
                       where p.Email == alumno.Email
                       select p;

            if (rows.ToList().Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Checks registered groups in current semester
        /// </summary>
        /// <returns>registered groups</returns>
        public int CantidadGruposRegistrados()
        {
            return 0;
        }

        /// <summary>
        /// registers a padron in group
        /// </summary>
        /// <param name="padron">user padron</param>
        /// <param name="numeroGrupo">group</param>
        /// <returns></returns>
        public void RegistrarPadronEnGrupo(string padron, int numeroGrupo)
        {
            return;
        }

        public IStudentRepository StudentRepository
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public ISubjectRepository SubjectRepository
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


		public ITicketRepository TicketRepository
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public IResponseRepository ResponseRepository
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

        public IGroupRepository GroupRepository
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public IList<Message> MessagesToSend { get; set; }

        public void SaveChanges()
        {
        }

        public bool TicketCreado(string tema)
        {
            return false;
        }

        public void CrearTicket(string tema, string mensaje, string estado, string attachment)
        {
            throw new NotImplementedException();
        }

        public void AgregarRespuestaTicket(string tema, string respuesta)
        {
            throw new NotImplementedException();
        }

        public void CambiarEstadoTicket(string tema, string estado)
        {
            throw new NotImplementedException();

        }
        public string ObtenerEstadoTicket(string tema)
        {
            throw new NotImplementedException();
        }

        public void CrearMail(string titulo, string mensaje, string destinatario, string copia)
        {
            throw new NotImplementedException();
        }

	}
}